package com.son.controller;

import com.son.domin.Student;
import com.son.domin.Teacher;
import com.son.factory.TeacherFactory;
import com.son.service.InterTeacherService;

import java.util.ArrayList;
import java.util.Scanner;

public class TeacherControllerImpl implements InterTeacherController {
       private Scanner sc = new Scanner(System.in);
private InterTeacherService interTeacherService= TeacherFactory.getTeacherService();
    @Override
    public void start() {
        teacherloop:
        while (true){
            System.out.println("--------欢迎来到<老师>管理系统-------");
            System.out.println("请输入您的选择：1 添加老师 2 删除老师 3 修改老师 4 查看老师 5 退出系统 ");
            String choice = sc.next();
            switch (choice){
                case "1":
                    addTeacher();
                    break;
                case "2":
                    deleteTeacher();
                    break;
                case "3":
                    updateTeacher();
                    break;
                case "4":
                    findAllTeacher();
                    break;
                case "5":
                    System.out.println("感谢您的使用，再见！");
                    break teacherloop;
                default:
                    System.out.println("您的输入有误，请重新输入");
                    break;
            }
        }
    }

    @Override
    public void addTeacher() {
        String sid = null;
        while (true) {
            System.out.println("请输入工号");
            sid = sc.next();
            boolean exists = interTeacherService.isExists(sid);
            if (exists) {
                System.out.println("已经存在，请重新输入");
            } else {
                break;
            }
        }
        Teacher teacher = luruSTeacherInfo(sid);
        interTeacherService.addTeacher(teacher);
        System.out.println("添加成功");
    }

    @Override
    public void updateTeacher() {
        String id = luruxuehao();
        Teacher teacher = luruSTeacherInfo(id);
        interTeacherService.updateTeacher(id,teacher);
    }

    @Override
    public void deleteTeacher() {
        String id = luruxuehao();
        interTeacherService.deleteTeacher(id);
        System.out.println("删除成功");
    }

    @Override
    public String luruxuehao() {
        String sid = null;
        while (true) {
            System.out.println("请输入工号");
            sid = sc.next();
            boolean exists = interTeacherService.isExists(sid);
            if (exists) {
                break;
            } else {
                System.out.println("已经存在，请重新输入");
            }
        }
        return sid;
    }

    @Override
    public void findAllTeacher() {
        ArrayList<Teacher> teachers = interTeacherService.findAllTeacher();
        if (teachers == null) {
            System.out.println("没有数据，请先添加");
            return;
        }
        System.out.println("学号\t姓名\t年龄\t生日");
        for (int i = 0; i < teachers.size(); i++) {
            Teacher teacher = teachers.get(i);
            if (teacher != null) {
                System.out.println(teacher.getSid() + teacher.getName() + teacher.getAge() + teacher.getBirthday());
            }
        }
    }

    @Override
    public Teacher luruSTeacherInfo(String sid) {
        System.out.println("请输入姓名");
        String name = sc.next();
        System.out.println("请输入年龄");
        String age = sc.next();
        System.out.println("请输入生日");
        String birthday = sc.next();
        Teacher teacher = new Teacher();
        teacher.setSid(sid);
        teacher.setName(name);
        teacher.setAge(age);
        teacher.setBirthday(birthday);
        return teacher;
    }
}
